package Lesson19.NotePad;

import java.util.Scanner;

public class SecuredNotepad extends SimpleNotePad implements ISecuredNotepad
{
	private String password;
	Scanner sc = new Scanner(System.in);
	
	public SecuredNotepad(int numberOfPages, String password, String confirmPassword) 
	{
		super(numberOfPages);
		if(validateNewPassword(password, confirmPassword))
		{
			this.password = password;
		}
		else
		{
			System.out.println("The passwords don't match!");
			System.exit(0);
		}
	}
	private boolean validateNewPassword(String password, String confirmPassword)
	{
		return password.equals(confirmPassword);
	}
	@Override
	public void addTextToPage(int pageNumber, String newText) 
	{
		if(requirePassword())
			super.addTextToPage(pageNumber, newText);
		else
			System.out.println("Wrong password! Access denied!");
	}
	@Override
	public void replaceTextToPage(int pageNumber, String newText) 
	{
		if(requirePassword())
			super.replaceTextToPage(pageNumber, newText);
		else
			System.out.println("Wrong password! Access denied!");
	}
	@Override
	public void deleteTextInPage(Page selectedPage) 
	{
		if(requirePassword())
			super.deleteTextInPage(selectedPage);
		else
			System.out.println("Wrong password! Access denied!");
	}
	@Override
	public void printAllPages() 
	{
		if(requirePassword())
			super.printAllPages();
		else
			System.out.println("Wrong password! Access denied!");
	}
	@Override
	public boolean requirePassword() 
	{
		System.out.println("Enter password to access this feature: ");
		return sc.next().equals(this.password);
	}
	
}
